package com.mycompany.sudoku.server;

import com.mycompany.sudoku.client.SudokuGenService;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

@SuppressWarnings("serial")
public class SudokuGenServiceImpl extends RemoteServiceServlet implements SudokuGenService {

	public static final boolean OPTION_SYMMETRICAL = true;
	// If you make the puzzles symmetrical, they are generally easier,
	// but much better looking :)
	
    Grid grid;		// The puzzle
    int[][] given;	// The numbers that are given at the start
    int[][] user;	// Numbers filled in by the user (may be wrong)
    Valids V;		// Collections of valid numbers for each square
    
    public int n;	// Dimension of a block (e.g. 3)
    public int N;	// Dimension of the puzzle (= n*n)

     
    public SudokuGenServiceImpl() {
    	this.n = 3;
    	this.N = this.n*this.n;
    	this.init();
    }

    public void init() {
	    // initialize stuff:
        this.grid = new Grid(this.n, this.N);
        this.user  = new int[this.N][this.N];
        this.given = new int[this.N][this.N];

    }

    
	@Override
	public int[][] generateGrid(int dificulty) {
    	// reset stuff:
        this.user  = new int[this.N][this.N];
        this.V = new Valids(this.n);

		this.grid.createSudoku();

        
        // Copy the given numbers to a second matrix
        // array 'given' represents the numbers that were given at the beginning.
        // any numbers the user derives, are put in a second array, 'user', because we
        // want to remember which numbers were given in the beginning.
        this.given = this.grid.given;
        for(int i=0; i<this.N; i++)
            for(int j=0; j<this.N; j++)
                this.user[i][j] = this.given[i][j];
        
        // Keep track of 'valids'
        this.V.update(this.given);
        
        // We're done.
		return this.given;
	}
}
